﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using 工作流XMLDomain修改工具.Codes;

namespace 工作流XMLDomain修改工具
{
    public partial class XML编辑界面 : Form
    {
        private Guid ProcessGUID;
        private DataRow ProcessData;
        public XML编辑界面(Guid ProcessGUID)
        {
            InitializeComponent();
            this.ProcessGUID = ProcessGUID;
            LoadData();
        }

        private void LoadData()
        {
            DataTable table = Business.GetProcessData(this.ProcessGUID);
            if (table.Rows.Count > 0)
            {
                ProcessData = table.Rows[0];
                textBox1.Text = ProcessData["ProcessGUID"].ToString();
                textBox2.Text = ProcessData["ProcessName"].ToString();
                textBox3.Text= ProcessData["PhoneFormMode"].ToString();


                try
                {
                    richTextBox1.Text = FormatXml(ProcessData["BT_DomainXML"].ToString());
                }
                catch
                {
                    MessageBox.Show("BT_DomainXML格式错误或存在未处理的特殊字符。");
                    this.Close();
                }

                try
                {
                    if (string.IsNullOrEmpty(ProcessData["PhoneFormDomainXml"].ToString())==false)
                    {
                        richTextBox2.Text = FormatXml(ProcessData["PhoneFormDomainXml"].ToString());
                    }
                    
                }
                catch
                {
                    MessageBox.Show("PhoneFormDomainXml格式错误或存在未处理的特殊字符。");
                    this.Close();
                }
            }
        }

        private string FormatXml(string xmlText)
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(xmlText);

            System.IO.StringWriter sw = new System.IO.StringWriter();
            using (System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(sw))
            {
                writer.Indentation = 2;  // the Indentation
                writer.Formatting = System.Xml.Formatting.Indented;
                doc.WriteContentTo(writer);
                writer.Close();
            }

            // out put the formated xml
            return sw.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ProcessGUID != Guid.Empty && ProcessData != null)
            {
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.LoadXml(richTextBox1.Text);
                }
                catch
                {
                    MessageBox.Show("BT_DomainXML文本格式错误。");
                    return;
                }

                XmlDocument doc1 = new XmlDocument();
                try
                {
                    doc1.LoadXml(richTextBox2.Text);
                }
                catch
                {
                    MessageBox.Show("PhoneFormDomainXml文本格式错误。");
                    return;
                }
                Business.UpdateDomainXML(ProcessGUID, doc.InnerXml, ProcessData["BT_DomainXML"].ToString(), doc1.InnerXml, ProcessData["PhoneFormDomainXml"].ToString(),textBox3.Text, ProcessData["PhoneFormMode"].ToString());
            }
            MessageBox.Show("修改成功。");
            this.Close();
        }

    }
}
